"""
@version:python3
@author:ll
@file:04-QWidget-顶层窗口操作案例.py
@time:2022/2/11 23:22
"""
# 0、导入需要的包和模块
from PyQt5.Qt import *
import sys

class Window(QWidget):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setWindowOpacity(0.8)
        self.setWindowTitle("顶层窗口操作案例")
        self.resize(500, 500)
        self.move_flag = False
        # 公共数据
        self.top_margin = 10
        self.btn_w = 80
        self.btn_h = 40

        self.setupui()

    def setupui(self):

        # 添加三个子控件
        close_btn = QPushButton(self)
        self.close_btn = close_btn
        close_btn.setText("关闭")
        close_btn.resize(self.btn_w, self.btn_h)


        max_btn = QPushButton(self)
        self.max_btn = max_btn
        max_btn.setText("最大化")
        max_btn.resize(self.btn_w, self.btn_h)


        mini_btn = QPushButton(self)
        self.mini_btn = mini_btn
        mini_btn.setText("最小化")
        mini_btn.resize(self.btn_w, self.btn_h)



        # def close():
        #     window.close()

        def max_normal():
            if self.isMaximized():
                self.showNormal()
                max_btn.setText("最大化")
            else:
                self.showMaximized()
                max_btn.setText("恢复")

        close_btn.pressed.connect(self.close)
        max_btn.pressed.connect(max_normal)
        mini_btn.pressed.connect(self.showMinimized)

    def resizeEvent(self, QResizeEvent):
        print("窗口大小发生了改变")
        # close_btn.setStyleSheet("background-color:red;")
        # close_btn_w = close_btn.width()
        window_w = self.width()
        close_btn_x = window_w - self.btn_w
        close_btn_y = self.top_margin
        # print(close_btn_w)
        self.close_btn.move(close_btn_x, close_btn_y)

        # max_btn_w = max_btn.width()
        max_btn_x = close_btn_x - self.btn_w
        max_btn_y = self.top_margin
        self.max_btn.move(max_btn_x, max_btn_y)

        mini_btn_x = max_btn_x - self.btn_w
        mini_btn_y = self.top_margin
        self.mini_btn.move(mini_btn_x, mini_btn_y)

    def mousePressEvent(self, QMouseEvent):
        # print("点击")
        if QMouseEvent.button() == Qt.LeftButton:
            self.move_flag = True
            # 确定两个点：鼠标按下时的点、窗口当前所在的原始点
            self.mouse_x = QMouseEvent.globalX()
            self.mouse_y = QMouseEvent.globalY()
            print(self.mouse_x,self.mouse_y)

            self.origin_x = self.x()
            self.origin_y = self.y()

    def mouseMoveEvent(self, QMouseEvent):
        # print("移动")
        if self.move_flag:
            # 计算移动向量
            move_x = QMouseEvent.globalX() - self.mouse_x
            move_y = QMouseEvent.globalY() - self.mouse_y
            print(move_x,move_y)
            dst_x = self.origin_x + move_x
            dst_y = self.origin_y + move_y
            self.move(dst_x,dst_y)

    def mouseReleaseEvent(self, QMouseEvent):
        self.move_flag = False





app = QApplication(sys.argv)


window = Window()

window.show()
window.setMouseTracking(True)

sys.exit(app.exec_())